#!/usr/bin/env python
# -*- encoding: utf8 -*-

import os
import sys
import random
import datetime
import settings
os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'
sys.path.append('../')
from mots.interns.models import *
from mots import settings
if settings.DATABASE_ENGINE is 'sqlite3':
    print 'Moving old DB to "'+settings.DATABASE_NAME+'.old"'
    try:
        os.rename(settings.DATABASE_NAME, settings.DATABASE_NAME+'.old')
    except OSError:
        print 'Old DB not found'

print 'Running "python manage.py syncdb --noinput"'
print '-' * 70
os.system('python manage.py syncdb --noinput')
print '-' * 70

print 'Creating superuser (login=test, password=test)'
admin = User.objects.create_user('test', 'admin@example.com', 'test')
admin.is_staff = True
admin.is_superuser = True
admin.save()


print 'Creating test data'
print 'Repository'
repo = svn_repo.objects.create(name='test', description='test', server_host='test', server_user='test', server_password='test', repo_path='test')
print 'Users'
users = []
for i in xrange(10):
    users += [svn_user.objects.create(name='user%d' % i, password='test')]
print 'Groups'
groups = []
for i in xrange(10):
    groups += [svn_group.objects.create(name='group%d' % i)]
print 'Projects'
projects = []
for i in xrange(2):
    projects += [svn_project.objects.create(name='project%d' % i, repo=repo)]
print 'Paths'
for project in projects:
    paths=[svn_path.objects.create(project=project, name='/', parent=None)]
    
    for i in xrange(10):
        tmp=random.choice(paths)
        paths+=[svn_path.objects.create(project=project, name='folder%d/'%i, parent=tmp)]
    